package com.android.fragmentdemo
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.util.SparseArray
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.hengfoxwolf.commonbase.base.util.LogX
import androidx.fragment.app.Fragment
import com.android.fragmentdemo.adbutil.ADBUtil
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databinding.ActivityMainBinding
import com.android.fragmentdemo.fragments.FirstPageFragment
import com.android.fragmentdemo.fragments.ListDataFragment
import com.android.fragmentdemo.fragments.MessageFragment
import com.android.fragmentdemo.fragments.MineFragment
import com.android.fragmentdemo.roomdb.RoomDBPTableUtil
import java.io.File

class MainActivity : BaseActivity<ActivityMainBinding>() {

    private lateinit var mFragmentList: SparseArray<Fragment>

    private var mCurrentCheckId = R.id.rb_home

    override fun getLayoutId() = R.layout.activity_main

    override fun init(savedInstanceState: Bundle?) {
        LogX.init(MainActivity::class.java)
        initView()
        LogX.d("activity---override fun init(savedInstanceState: Bundle?)")
        val simpleName0 =  mBinding.rlMainRoot.layoutParams::class.java.name
        val simpleName1 =  mBinding.rlMainRoot.layoutParams::class.java.canonicalName
        val simpleName2 =  mBinding.rlMainRoot.layoutParams::class.java.`package`
        val simpleName3 =  mBinding.rlMainRoot.layoutParams::class.java.simpleName
        Log.d(TAG, "simpleName0:${simpleName0}---simpleName1:${simpleName1}---simpleName2:${simpleName2}---simpleName3:${simpleName3}")

        Log.d(TAG, "cacheDir.absolutePath:${cacheDir.absolutePath}")
        Log.d(TAG, "filePath:${filesDir.absolutePath}")
        Log.d(TAG, "externalStorageDirectory:${Environment.getExternalStorageDirectory().absolutePath}")
        Log.d(TAG, "File.separator:${File.separator}")

        //作占位符用，优先且确保Migration相关代码只执行一次。
        RoomDBPTableUtil.queryAll()
    }

    private fun initView() {
        //ADBUtil.enableAccessibilityService(this)

        mFragmentList = SparseArray()
        mFragmentList.append(R.id.rb_home, FirstPageFragment.newInstance("这是我的首页"))
        mFragmentList.append(R.id.rb_list, ListDataFragment.newInstance("这是列表选项"))
        mFragmentList.append(R.id.rb_message, MessageFragment.newInstance("这是信息9088"))
        mFragmentList.append(R.id.rb_mine, MineFragment.newInstance("这是我的我的"))

        mBinding.rgBottomTab.setOnCheckedChangeListener { _, checkId ->
            if (mCurrentCheckId != checkId) {
                val ft = supportFragmentManager.beginTransaction()
                ft.replace(R.id.fl_frame, mFragmentList.get(checkId))
                ft.commit()
                mCurrentCheckId = checkId
            }
        }

        val mTransaction = supportFragmentManager.beginTransaction()
        mTransaction.add(R.id.fl_frame, mFragmentList.get(R.id.rb_home)).commit()
        mBinding.rbHome.isChecked = true

        //startActivity<BackgroundTransparentActivity>()
    }

    override fun onDestroy() {
        super.onDestroy()
        //ADBUtil.disableAccessibilityService(this)
        //Log.d(
        //    TAG,
        //    "override fun onDestroy()--enableFlag:${ADBUtil.getAccessibilityServiceEnableFlag(this)}"
        //)
    }

    //销毁所有的Fragment
    fun destroyAllFragment() {
        if (!supportFragmentManager.isDestroyed) {
            val fragments = supportFragmentManager.fragments
            val transaction = supportFragmentManager.beginTransaction()
            for (fragment in fragments) {
                if (fragment != null) {
                    transaction.remove(fragment)
                    transaction.commit()
                }
            }
        }
    }

    companion object {
        const val TAG = "MainActivity"
    }
}